package kr.co.graviti.adm.auth.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import kr.co.graviti.adm.common.dao.AdmCommonDAO;
import kr.co.graviti.common.action.BaseFuncAction;
import kr.co.graviti.common.util.CommonUtil;
import kr.co.graviti.common.util.Crypto;


public class AdmAuthAction extends BaseFuncAction {
	
	private static final Logger _log = Logger.getLogger(AdmAuthAction.class);
	
	public ActionForward execute(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,HttpServletResponse response)
	        throws Exception {
			 
		HashMap req = null;
		req = CommonUtil.getRequest(request);
		String passwd = (String)req.get("passwd");
		String decodePwd = new Crypto().encryption(passwd);
		req.put("passwd", decodePwd);
		String lang = CommonUtil.strNullChk((String)req.get("lang"));
		boolean result;
		
		int cnt = new AdmCommonDAO().getIntegerDAO(req, "admauth.selectAdmInfoCnt");
		if(cnt > 0){
			HashMap map = new AdmCommonDAO().getHashMapDAO(req, "admauth.selectAdmInfo");
			
			HttpSession session = request.getSession();
			
			session.setAttribute("admId", map.get("USERID"));
			session.setAttribute("admNm", map.get("USERNM"));
			session.setAttribute("lang", lang);
			
			// Topmenu session으로 굽기 
			String ibatisNm = "admauth";
			List topMenuList = new AdmCommonDAO().getArrayListDAO(req,ibatisNm + ".selectTopMenuList");
			
			JSONArray jsonArray = JSONArray.fromObject(topMenuList);
			Map topMemuMap = new HashMap();
			topMemuMap.put("topmenu", jsonArray);
			JSONObject jsonObject = JSONObject.fromObject(topMemuMap);
			session.setAttribute("topmenu", jsonObject);
			session.setAttribute("topmenulength", Integer.toString(topMenuList.size()));
			
			List LeftMenuList = new AdmCommonDAO().getArrayListDAO(req,ibatisNm + ".selectLeftMenuList");
			JSONArray jsonArrayLeft = JSONArray.fromObject(LeftMenuList);
			Map leftMemuMap = new HashMap();
			leftMemuMap.put("leftmenu", jsonArrayLeft);
			JSONObject jsonObjectLeft = JSONObject.fromObject(leftMemuMap);
			session.setAttribute("leftmenu", jsonObjectLeft);
			
			req.put("userip",request.getRemoteAddr());
			new AdmCommonDAO().setInsertDAO(req, "admauth.insertLoginLogs");
			_log.debug("=====>"+session.getAttribute("admId"));
			_log.debug("=====>"+session.getAttribute("admNm"));
			_log.debug("=====>"+session.getAttribute("lang"));
			
			session.setMaxInactiveInterval(60*60*14);
			result = true;
		}else{
			result = false;
		}
		Boolean objBool = Boolean.valueOf(result);
		request.setAttribute("result", objBool);
		
		_log.info("===>"+objBool);
		
		return mapping.findForward("success");
	}
	
	
	
	
}
